package com.ptszyxx.popose.module.main.message;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.http.data.AppViewModelFactory;
import com.ptszyxx.popose.databinding.FragmentMessageBinding;
import com.ptszyxx.popose.module.main.message.vm.MessageVM;
import com.tencent.qcloud.tuikit.tuiconversation.bean.ConversationInfo;
import com.tencent.qcloud.tuikit.tuiconversation.classicui.interfaces.OnConversationAdapterListener;
import com.tencent.qcloud.tuikit.tuiconversation.presenter.ConversationPresenter;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.entity.notice.NoticeLastResult;

import java.util.List;

/**
 * 会话列表参考：TUIConversationFragment
 */
public class MessageFragment extends BaseFragment<FragmentMessageBinding, MessageVM> {
    private ConversationPresenter presenter;

    public static MessageFragment getInstance() {
        return new MessageFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_message;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MessageVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MessageVM.class);
    }

    @Override
    public void initView() {
        super.initView();
        presenter = new ConversationPresenter();
        presenter.setConversationListener();
        presenter.setShowType(ConversationPresenter.SHOW_TYPE_CONVERSATION_LIST_WITH_FOLD);
        binding.conversationLayout.setPresenter(presenter);
        // 会话列表面板的默认UI和交互初始化
        binding.conversationLayout.initDefault();
        binding.conversationLayout.getConversationList().setOnConversationAdapterListener(new OnConversationAdapterListener() {
            @Override
            public void onItemClick(View view, int viewType, ConversationInfo conversationInfo) {
//                TUIConversationUtils.startChatActivity(conversationInfo);
                YActivityUtil.getInstance().jumpChat(viewModel, conversationInfo.getId(), conversationInfo.getTitle(), conversationInfo.getIconPath());
            }

            @Override
            public void onItemLongClick(View view, ConversationInfo conversationInfo) {
            }

            @Override
            public void onConversationChanged(List<ConversationInfo> dataSource) {
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestNoticeLast();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, o -> {
            presenter.loadMoreConversation();
        });
        viewModel.uc.onNoticeEvent.observe(this, o -> {
            setNoticeUI((NoticeLastResult)o);
        });
    }

    private void setNoticeUI(NoticeLastResult result) {
        if (result == null){
            return;
        }
        if (result.getXitong() != null){
            binding.tvContentSystem.setText(result.getXitong().getTitle());
            binding.tvTimeSystem.setText(result.getXitong().getCreatetime());
        }
        if (result.getPingtai() != null){
            binding.tvContentPlatform.setText(result.getPingtai().getTitle());
            binding.tvTimePlatform.setText(result.getPingtai().getCreatetime());
        }
    }


}
